<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #0f9876">
    <div style="display: flex; background-color: white; width: 50%; border-radius: 10px; overflow: hidden">
      <div style="flex: 1">
        <img src="@/assets/login.png" alt="" style="width: 100%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center;">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="loginRef">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px">
            欢迎登录后台管理系统
          </div>
          <!--账号-->
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号..."
                      v-model="user.username"></el-input>
          </el-form-item>
          <!--密码-->
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码..."
                      v-model="user.password"></el-input>
          </el-form-item>
          <!--验证码-->
          <el-form-item prop="code">
            <div style="display: flex">
              <el-input prefix-icon="el-icon-circle-check" size="medium" style="flex: 1" placeholder="请输入验证码..."
                        v-model="user.code"></el-input>
              <div style="flex: 1; height: 36px">
                <ValidCode @input="getCode"></ValidCode>
              </div>
            </div>
          </el-form-item>
          <!--登录按钮-->
          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="login">登 录</el-button>
          </el-form-item>
          <div style="display: flex">
            <!--注册入口-->
            <div style="flex: 1">还没有注册账号？请<span style="color: #0f9876; cursor: pointer" @click="$router.push('/register')">注册</span></div>
            <!--忘记密码-->
            <div style="flex: 1; text-align: right"><span style="color: #0f9876; cursor: pointer">忘记密码</span></div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import ValidCode from '@/components/ValidCode'

export default {
  name: "Login",
  components: {
    ValidCode
  },
  data() {
    //验证码校验（自定义）
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if (value.toLowerCase() !== this.code) {
        callback(new Error('验证码错误'))
      }else{
        callback()
      }
    };
    return {
      code: '',//验证码组件里面传来的code
      user: {
        username: '',
        password: '',
        code: '',//表单里用户输入的验证码
      },
      rules: {
        username: [
            { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        code: [
            { validator: validateCode, trigger: 'blur' },

        ]
      }
    }
  },
  methods: {
    getCode(code) {
      console.log(code);
      this.code = code.toLowerCase(); // 保存验证码组件输入的验证码
    },
    login() {
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          //验证通过
          this.$request.post('/login', this.user).then(res => {
            if (res.code === '200') {
              this.$router.push('/')
              this.$message.success('登录成功！')
              localStorage.setItem("honey-user", JSON.stringify(res.data))//存储用户数据

            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
  }
}
</script>

<style scoped>
/* 你的样式 */
</style>